/*
 * Copyright (C) 2014 Francisco José Morero Peyrona. All Rights Reserved.
 *
 * This file is part of JStatViewer project: https://code.google.com/p/jstat-viewer/
 *
 * GNU Classpath is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the free
 * Software Foundation; either version 3, or (at your option) any later version.
 *
 * Tapas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * Tapas; see the file COPYING.  If not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package jstatviewer.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import jstatviewer.Util;
import jstatviewer.reader.JStatFileReader;
import jstatviewer.reader.JStats;
import org.math.plot.Plot2DPanel;

/**
 * A JPanel containing a Plot2DPanel component.
 * <p>
 * JPanel contains instead of inheriting to take advantage of encapsulation:
 * only desired functionality is exposed.
 * 
 * @author peyrona
 */
final class GraphPanel extends JPanel
{
    private final Plot2DPanel plotPanel = new Plot2DPanel();

    //----------------------------------------------------------------------------//

    GraphPanel()
    {
        setBorder( new EmptyBorder( 5, 5, 5, 5 ) );
        setLayout( new BorderLayout( 0,5 ) );
        add( plotPanel, BorderLayout.CENTER );

        plotPanel.addLegend( "SOUTH" );
    }

    //----------------------------------------------------------------------------//

    /**
     * Add values stored in passed log file to current graph.
     *
     * @param fJStatsLog  Can not be null
     * @param sDateFormat Can be null
     */
    void show( File fJStatsLog, int nTimeInterval )
    {
        try
        {
            JStatFileReader fReader = new JStatFileReader( fJStatsLog );
            JStats          stats   = fReader.parse();

            plot( stats, nTimeInterval );
        }
        catch( IOException ioe )
        {
            Util.showError( "Can't read file.\n"+ ioe.getLocalizedMessage() );
        }
    }

    //----------------------------------------------------------------------------//

    private void plot( final JStats stats, final int nTimeInterval)
    {
        EventQueue.invokeLater( new Runnable()
        {
            @Override
            public void run()
            {
                for( String sColName : stats.getColumnNames() )
                {
                    final double[] y = createYValues( stats.getValuesFor( sColName ) );
                    final double[] x = createXValues( y.length, nTimeInterval );

                    plotPanel.addLinePlot( sColName, x, y );
                }
            }
        } );

        if( stats.getLinesNotProcessed() > 0 )
        {
            Util.showInfo( "Unable to process "+ stats.getLinesNotProcessed() +" lines" );
        }
    }

    private double[] createXValues( int length, double increment )
    {
        double[] anValues = new double[ length ];
        double   value    = 0;

        for( int n = 0; n < anValues.length; n++ )
        {
            anValues[n] = value;
            value += increment;
        }

        return anValues;
    }

    private double[] createYValues( List<Double> lstValues )
    {
        double[] anValues = new double[ lstValues.size() ];

        for( int n = 0; n < anValues.length; n++ )
        {
            anValues[n] = lstValues.get( n );
        }

        return anValues;
    }
}